<template>
  <div class="course-info">
    <van-cell-group :style="styleOptions">
      <!-- 课程头部 -->
      <van-cell class="course-img">
        <img :src="course.courseImgUrl" alt="详情图片"/>
      </van-cell>
      <!-- 课程基础信息 -->
      <van-cell class="course-description">
        <!-- 课程名 -->
        <h2 v-text="course.courseName"></h2>
        <!-- 课程概述 -->
        <p v-text="course.previewFirstField"></p>
        <!-- 课程销售信息 -->
        <div class="course-sale-info">
          <p class="course-price">
            <span class="discounts">￥{{course.discounts}}</span>
            <span>￥{{course.price}}</span>
          </p>
          <span class="tag">{{course.sales}}人已经购买</span>
          <span class="tag">每周三、周五更新</span>
        </div>
      </van-cell>
      <!-- 课程详情 -->
      <van-cell>
        <van-tabs sticky scrollspy>
          <van-tab title="详情">
            <div v-html="course.courseDescription"></div>
          </van-tab>
          <van-tab title="详情">
            <course-sections
              v-for="item in sections"
              :key="item.id"
              :section-data="item"
            />
          </van-tab>
        </van-tabs>
      </van-cell>
    </van-cell-group>
    <!-- 底部支付区域 -->
    <van-tabbar v-if="!course.isBuy">
      <div class="price">
        <span v-text="course.discountsTag"></span>
        <span class="discounts">￥{{ course.discounts }}</span>
        <span>￥{{ course.price }}</span>
      </div>
      <van-button type="primary">立即购买</van-button>
    </van-tabbar>
  </div>
</template>

<script>
import { getCourseById, getSectionAndLesson } from '@/services/course'
import CourseSections from './components/CourseSections'

export default {
  name: 'CourseInfo',
  data () {
    return {
      course: {},
      // 课程章节信息
      sections: [],
      styleOptions: {}
    }
  },
  props: {
    courseId: {
      type: [String, Number],
      required: true
    }
  },
  components: {
    CourseSections
  },
  created () {
    this.loadCourse()
    this.loadSections()
  },
  methods: {
    async loadCourse () {
      const { data } = await getCourseById({
        courseId: this.courseId
      })
      this.course = data.content
      if (data.content.isBuy) {
        this.styleOptions.bottom = 0
      }
    },
    async loadSections () {
      const { data } = await getSectionAndLesson({
        courseId: this.courseId
      })
      this.sections = data.content.courseSectionList
    }
  }
}
</script>

<style scoped>
.van-cell{
  padding: 0;
}
.course-img{
  height: 280px;
}

/* 课程描述区域 */
.course-description {
  padding: 10px 20px;
  height: 150px;
}
.course-sale-info {
  display: flex;
}
.course-sale-info .course-price {
  flex: 1;
  margin: 0;
}
.discounts{
  color: #ff7452;
  font-size: 24px !important;
  font-weight: bolder;
}
.course-sale-info .tag{
  background: #f8f9fa;
  font-size: 12px;
  font-weight: 700;
  color: #666;
  margin-left: 7px;
  padding: 7px;
  line-height: 15px;
  border-radius: 13px;
}
/* 底部支付区域后设置 */
.van-cell-group {
  width: 100%;
  position: fixed;
  top: 0;
  bottom: 50px;
  overflow-y: auto;
}
.van-tabbar {
  line-height: 50px;
  padding: 0 20px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.price span{
  font-size: 14px;
}
.van-button{
  width: 50%;
  height: 80%;
}

</style>
